/*
DESCRIPTION
This do file creates the figures in the appendix.

INPUT
	data/FormsHits.dta
	data/FormsSim.dta
	data/BITTrialAdoption.dta
	data/Survey.dta
	data/Forecasts.dta

OUTPUT
	figures/appendix/form_hit_by_pre_BIT_wgt.pdf
	figures/appendix/form_sim_by_pre_BIT_wgt.pdf
	figures/appendix/adoption_by_respdaysbin10.pdf
	figures/appendix/adoption_by_respnumbin10.pdf
	figures/appendix/adoption_by_rec_tbin.pdf
	figures/appendix/adoption_by_preexist_controlcomm.pdf
	figures/appendix/adoption_by_preexist_tebin3.pdf
	figures/appendix/ROC.pdf
	figures/appendix/adoption_by_sig_staff.pdf
	figures/appendix/adoption_by_te_staff.pdf
	figures/appendix/adoption_by_staff_preexist.pdf
	figures/appendix/adoption_by_sig_ct.pdf
	figures/appendix/adoption_by_te_ct.pdf
	figures/appendix/adoption_by_ct_preexist.pdf
	figures/appendix/survey_by_group.pdf
	figures/appendix/forecasts_ranking.pdf
*/

*****************************
***FIGURE A2: ONLINE FORMS***
*****************************
{ 
	{ // A2a
		clear all
		use "data/FormsHits.dta"
		
		mat bar=J(4,8,.)
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt
		
		qui reg hit ibn.insample#ibn.comm_preexisted [aweight=weight], cl(matchcityid) nocons
		
		local r=1
		forvalues bit=1(-1)0 {
			forvalues pre=0/1 {
				mat bar[`r',1]=`bit'
				mat bar[`r',2]=`pre'
				mat bar[`r',3]=_b[`bit'.insample#`pre'.comm_preexisted]
				mat bar[`r',4]=_b[`bit'.insample#`pre'.comm_preexisted]+invttail(e(df_r),0.975)*_se[`bit'.insample#`pre'.comm_preexisted]
				mat bar[`r',5]=_b[`bit'.insample#`pre'.comm_preexisted]+invttail(e(df_r),0.025)*_se[`bit'.insample#`pre'.comm_preexisted]
				qui tabstat weight if insample==`bit' & comm_preexisted==`pre', statistic(sum) save
				mat bar[`r',6]=r(StatTotal)[1,1]
				qui tabstat hit_wgt if insample==`bit' & comm_preexisted==`pre' & hit==1, statistic(sum) save
				local stat=string(r(StatTotal)[1,1],"%3.0f")
				mat bar[`r',7]=`stat'
				local ++r
			}
		}

		qui test 1.insample#0.comm_preexisted = 1.insample#1.comm_preexisted
		local pval1=string(`r(p)',"%3.2f")
		qui test 0.insample#0.comm_preexisted = 0.insample#1.comm_preexisted
		local pval0=string(`r(p)',"%3.2f")
		qui test 1.insample#0.comm_preexisted - 1.insample#1.comm_preexisted = 0.insample#0.comm_preexisted - 0.insample#1.comm_preexisted
		local pvald=string(`r(p)',"%3.2f")

		qui test 0.insample#0.comm_preexisted = 1.insample#0.comm_preexisted
		local pval0b=string(`r(p)',"%3.2f")
		qui test 0.insample#1.comm_preexisted = 1.insample#1.comm_preexisted
		local pval1b=string(`r(p)',"%3.2f")

		qui test 1.insample#0.comm_preexisted = 1.insample#1.comm_preexisted
		local pval13=string(`r(p)',"%3.2f")
		qui test 0.insample#0.comm_preexisted = 0.insample#1.comm_preexisted
		local pval24=string(`r(p)',"%3.2f")

		qui test 0.insample#0.comm_preexisted - 1.insample#0.comm_preexisted = 0.insample#1.comm_preexisted - 1.insample#1.comm_preexisted
		local pvaldb=string(`r(p)',"%3.2f")

		* record unweighted hit rate
		qui count if insample==0 & comm_preexisted==0
		local NNew=`r(N)'
		qui count if insample==0 & comm_preexisted==1
		local NPre=`r(N)'
		qui count if insample==0 & comm_preexisted==0 & hit==1
		local HitNew=`r(N)'
		qui count if insample==0 & comm_preexisted==1 & hit==1
		local HitPre=`r(N)'
		
		svmat bar, names(col)
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
			
		gen byte zero=0
		
		gsort bin2 -bin1
		gen 	x=_n*0.3+(bin2)*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		local topvar p_adopt
		qui sum p_adopt if bin2==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin2==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin2==0 & bin1==1
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin2==0 & bin1==0
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin2==1 & bin1==1
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin2==1 & bin1==0
		local ystart11=`r(max)'+2
		
		qui sum x if bin2==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		
		qui sum x if bin2==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'

		local Ystart1=`yend0'+4
		local Ystart2=`yend0'+4
		local Ystart4=`yend1'+4
		local Yend24=max(`Ystart2',`Ystart4')+4
		local Ystart3=`Yend24'+4
		local Yend13=max(`Ystart1',`Ystart3')+4
		local Xcent13=(`xstart0'+`xstart1')/2
		local Xcent24=(`xend0'+`xend1')/2
		
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				`rcap' ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  	`yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  	`yend1' `xstart1' `yend1' `xend1' ///
						`Ystart4' `xend1' `Yend24' `xend1'  	 `Ystart2' `xend0' `Yend24' `xend0'  	`Yend24' `xend1' `Yend24' 	`xend0' ///
						`Ystart1' `xstart0' `Yend13' `xstart0'  `Ystart3' `xstart1' `Yend13' `xstart1'  	`Yend13' `xstart0' `Yend13' 	`xstart1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`yend0' `xcent0' "{it:p}=`pval0b'", size(small) placement(n)) ///
				text(`yend1' `xcent1' "{it:p}=`pval1b'", size(small) placement(n)) /// 
				text(`Yend13' `Xcent13' "{it:p}=`pval13'", size(small) placement(n)) ///
				text(`Yend24' `Xcent24' "{it:p}=`pval24'", size(small) placement(n)) ///
				text(104 `xcent0' "{bf:City depts. in BIT trials}" "{bf:with {it:new} comm.}", placement(s)) ///
				text(104 `xcent1' "{bf:City depts. in BIT trials}" "{bf:with {it:pre-existing} comm.}", placement(s)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 100)) yticks(0(10)100) ylabel(0(10)100, gmax) ///
				xlabel(0.3 "{it:BIT}" 0.6 "{it:Matched}" 1.1 "{it:BIT}" 1.4 "{it:Matched}", noticks) ///
				legend(off) ///
				xtitle(" " "{bf:City department from BIT trial or from matched non-BIT city?}") ///
				ytitle("Percent with downloadable forms/communications") ///
				note("Unweighted number of forms downloaded/total attempts for websites of non-BIT city departments:" ///
						"New: `HitNew'/`NNew', Pre-existing: `HitPre'/`NPre'")
		graph export "figures/appendix/form_hit_by_pre_BIT_wgt.pdf", replace
	}
	{ // A2b
		clear all
		use "data/FormsSim.dta"
		
		collapse (mean) sim_cos (first) trialnumber comm_preexisted insample matchcityid, by(cityid form)
			
		egen hitN_bytrialnumber=count(form), by(trialnumber insample)
		gen weight=1/hitN_bytrialnumber
		
		label var hitN_bytrialnumber "For non-BIT cities: # hits of forms/comms matched to this trial"
		label var weight "1/hitN_bytrialnumber if non-BIT; 1 if in BIT trial sample"
		
		mat bar=J(4,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt
		
		qui reg sim_cos ibn.insample#ibn.comm_preexisted [aweight=weight], cl(matchcity) nocons
		
		local r=1
		forvalues bit=1(-1)0 {
			forvalues pre=0/1 {
				mat bar[`r',1]=`bit'
				mat bar[`r',2]=`pre'
				mat bar[`r',3]=_b[`bit'.insample#`pre'.comm_preexisted]
				mat bar[`r',4]=_b[`bit'.insample#`pre'.comm_preexisted]-invnorm(0.975)*_se[`bit'.insample#`pre'.comm_preexisted]
				mat bar[`r',5]=_b[`bit'.insample#`pre'.comm_preexisted]+invnorm(0.975)*_se[`bit'.insample#`pre'.comm_preexisted]
				qui count if insample==`bit' & comm_preexisted==`pre'
				mat bar[`r',6]=r(N)
				local ++r
			}
		}

		qui test 1.insample#0.comm_preexisted = 1.insample#1.comm_preexisted
		local pval1=string(`r(p)',"%3.2f")
		qui test 0.insample#0.comm_preexisted = 0.insample#1.comm_preexisted
		local pval0=string(`r(p)',"%3.2f")
		qui test 1.insample#0.comm_preexisted - 1.insample#1.comm_preexisted = 0.insample#0.comm_preexisted - 0.insample#1.comm_preexisted
		local pvald=string(`r(p)',"%3.2f")

		qui test 0.insample#0.comm_preexisted = 1.insample#0.comm_preexisted
		local pval0b=string(`r(p)',"%3.2f")
		qui test 0.insample#1.comm_preexisted = 1.insample#1.comm_preexisted
		local pval1b=string(`r(p)',"%3.2f")
		qui test 0.insample#0.comm_preexisted - 1.insample#0.comm_preexisted = 0.insample#1.comm_preexisted - 1.insample#1.comm_preexisted
		local pvaldb=string(`r(p)',"%3.2f")

		qui test 1.insample#0.comm_preexisted = 1.insample#1.comm_preexisted
		local pval13=string(`r(p)',"%3.2f")
		qui test 0.insample#0.comm_preexisted = 0.insample#1.comm_preexisted
		local pval24=string(`r(p)',"%3.2f")
		
		svmat bar, names(col)
		
		replace ub=ub
		replace lb=lb
		
		tostring p_adopt, gen(barlabel) format("%3.2f") force
		replace barlabel=barlabel
		
		tostring N, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ({it:N}="+N+")"
			
		egen max=max(p_adopt), by(bin1 bin2)
		replace max=max+1/100
		gen byte zero=0
		
		gsort bin2 -bin1
		gen 	x=_n*0.3+(bin2)*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		local topvar p_adopt
		qui sum p_adopt if bin2==0
		local yend0=`r(max)'+5/100
		qui sum p_adopt if bin2==1
		local yend1=`r(max)'+5/100
		qui sum p_adopt if bin2==0 & bin1==1
		local ystart00=`r(max)'+2/100
		qui sum p_adopt if bin2==0 & bin1==0
		local ystart01=`r(max)'+2/100
		qui sum p_adopt if bin2==1 & bin1==1
		local ystart10=`r(max)'+2/100
		qui sum p_adopt if bin2==1 & bin1==0
		local ystart11=`r(max)'+2/100
		
		qui sum x if bin2==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		
		qui sum x if bin2==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'

		local Ystart1=`yend0'+4/100
		local Ystart2=`yend0'+4/100
		local Ystart4=`yend1'+4/100
		local Yend24=max(`Ystart2',`Ystart4')+4/100
		local Ystart3=`Yend24'+4/100
		local Yend13=max(`Ystart1',`Ystart3')+4/100
		local Xcent13=(`xstart0'+`xstart1')/2
		local Xcent24=(`xend0'+`xend1')/2
		
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				`rcap' ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  	`yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  	`yend1' `xstart1' `yend1' `xend1' ///
						`Ystart4' `xend1' `Yend24' `xend1'  	 `Ystart2' `xend0' `Yend24' `xend0'  	`Yend24' `xend1' `Yend24' 	`xend0' ///
						`Ystart1' `xstart0' `Yend13' `xstart0'  `Ystart3' `xstart1' `Yend13' `xstart1'  	`Yend13' `xstart0' `Yend13' 	`xstart1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`yend0' `xcent0' "{it:p}=`pval0b'", size(small) placement(n)) ///
				text(`yend1' `xcent1' "{it:p}=`pval1b'", size(small) placement(n)) /// 
				text(`Yend13' `Xcent13' "{it:p}=`pval13'", size(small) placement(n)) ///
				text(`Yend24' `Xcent24' "{it:p}=`pval24'", size(small) placement(n)) ///
				text(1.35 `xcent0' "{bf:City depts. in BIT trials}" "{bf:with {it:new} comm.}", placement(s)) ///
				text(1.35 `xcent1' "{bf:City depts. in BIT trials}" "{bf:with {it:pre-existing} comm.}", placement(s)) ///
				graphregion(color(white) margin(2 2 2 12)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 1) noextend) yticks(0(0.1)1) ylabel(0(0.1)1, gmax) ///
				xlabel(0.3 "{it:BIT}" 0.6 "{it:Matched}" 1.1 "{it:BIT}" 1.4 "{it:Matched}", noticks) ///
				legend(off) ///
				xtitle(" " "{bf:City department from BIT trial or from matched non-BIT city?}") ///
				ytitle("Average cosine similarity between years", placement(s))
		graph export "figures/appendix/form_sim_by_pre_BIT_wgt.pdf", replace
	}
}

*******************************************
***FIGURE A3: ADOPTION BY RESPONSE TIMES***
*******************************************
{
	{ // A3a
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1
		
		qui sum responsetime
		local xmax=ceil(`r(max)'/10)*10
		binscatter2 adopted responsetime, ///
			nquantiles(10) ///
			yscale(range(0 0.7)) ///
			yticks(0(0.1)0.7) ///
			ylabel(0 "0" 0.1 "10" 0.2 "20" 0.3 "30" 0.4 "40" 0.5 "50" 0.6 "60" 0.7 "70") ///
			xtitle("Days until response") ///
			ytitle("Percent of trials adopted") ///
			note("Binscatter") graphregion(color(white))
		graph export "figures/appendix/adoption_by_respdaysbin10.pdf", replace
	}
	{ // A3b
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1
		
		qui sum contactednum
		local xmax=ceil(`r(max)'/10)*10
		binscatter2 adopted contactednum, ///
			nquantiles(10) ///
			yscale(range(0 0.7)) ///
			yticks(0(0.1)0.7) ///
			ylabel(0 "0" 0.1 "10" 0.2 "20" 0.3 "30" 0.4 "40" 0.5 "50" 0.6 "60" 0.7 "70") ///
			xtitle("Number of contacts until response") ///
			ytitle("Percent of trials adopted") ///
			note("Binscatter") graphregion(color(white))
		graph export "figures/appendix/adoption_by_respnumbin10.pdf", replace
	}
}

************************************
***FIGURE A4: BIT RECOMMENDATIONS***
************************************
{
	clear all
	use "data/BITTrialAdoption.dta", clear
	keep if sample==1

	gen	byte tbin=0 if treatmenteffect_maxt<=0
	replace	tbin=1 if treatmenteffect_maxt>0 & treatmenteffect_maxt<invnorm(0.975)
	replace	tbin=2 if treatmenteffect_maxt>=invnorm(0.975)

	label define tbin 0 "Zero or negative" 1 "Positive & insignificant" 2 "Positive & significant"
	label values tbin tbin

	mat bar=J(3,7,.) 
	mat colnames bar = bin p_adopt lb ub N N_adopt N_nadopt

	qui reg recommendation_adopt ibn.tbin, cl(cityid) nocons
	forvalues b=0/2 {
		mat bar[`b'+1,1]=`b'
		mat bar[`b'+1,2]=_b[`b'.tbin]
		mat bar[`b'+1,3]=_b[`b'.tbin]+invttail(e(df_r),0.975)*_se[`b'.tbin]
		mat bar[`b'+1,4]=_b[`b'.tbin]+invttail(e(df_r),0.025)*_se[`b'.tbin]
		qui count if tbin==`b' & recommendation_adopt!=.
		mat bar[`b'+1,5]=r(N)
		qui count if tbin==`b' & recommendation_adopt==1
		mat bar[`b'+1,6]=r(N)
		qui count if tbin==`b' & recommendation_adopt==0
		mat bar[`b'+1,7]=r(N)
	}

	*pvals
	tab tbin recommendation_adopt if inlist(tbin,0,1), exact
	local pval_01=string(`r(p_exact)',"%3.2f")
	tab tbin recommendation_adopt if inlist(tbin,1,2), exact
	local pval_12=string(`r(p_exact)',"%3.2f")
	tab tbin recommendation_adopt if inlist(tbin,0,2), exact
	local pval_02=string(`r(p_exact)',"%3.2f")

	clear
	svmat bar, names(col)
	
	replace p_adopt=p_adopt*100
	replace ub=ub*100
	replace lb=lb*100
	
	tostring p_adopt, gen(barlabel) format("%2.0f") force
	replace barlabel=barlabel+"%"
	
	tostring N, replace format("%3.0f") force
	tostring N_adopt, replace format("%3.0f") force
	
	replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
	
	qui sum p_adopt
	local maxy=(ceil(`r(max)'/10)+1)*10
	
	egen max=max(p_adopt), by(bin)
	replace max=max+1
	
	sort bin
	gen 	x=_n*0.3
	qui sum x
	local mx=`r(max)'+0.15
	
	gen byte zero=0
	
	local group1 0
	local group2 1
	qui sum p_adopt if bin==`group1'
	local y1start_01=`r(max)'+3
	qui sum p_adopt if bin==`group2'
	local y2start_01=`r(max)'+3
	local y1start_01=max(`y1start_01',`y2start_01')
	local y2start_01=`y1start_01'
	local yend_01=`y1start_01'+3
	qui sum x if bin==`group1'
	local x1_01=`r(mean)'
	qui sum x if bin==`group2'
	local x2_01=`r(mean)'
	local xcent_01=(`x1_01'+`x2_01')/2
	
	local group1 1
	local group2 2
	qui sum p_adopt if bin==`group1'
	local y1start_12=`r(max)'+3
	qui sum p_adopt if bin==`group2'
	local y2start_12=`r(max)'+3
	local y1start_12=max(`y1start_12',`y2start_12',`y1start_01')
	local y2start_12=`y1start_12'
	local yend_12=`y1start_12'+3
	qui sum x if bin==`group1'
	local x1_12=`r(mean)'
	qui sum x if bin==`group2'
	local x2_12=`r(mean)'
	local xcent_12=(`x1_12'+`x2_12')/2
	
	local group1 0
	local group2 2
	qui sum p_adopt if bin==`group1'
	local y1start_02=`r(max)'+3
	qui sum p_adopt if bin==`group2'
	local y2start_02=`r(max)'+3
	local y1start_02=max(`y1start_02',`y2start_02',`y1start_12')+5
	local y2start_02=`y1start_02'
	local yend_02=`y1start_02'+3
	qui sum x if bin==`group1'
	local x1_02=`r(mean)'
	qui sum x if bin==`group2'
	local x2_02=`r(mean)'
	local xcent_02=(`x1_02'+`x2_02')/2
	
	twoway 	(bar p_adopt x if bin==0, barwidth(0.175) lcolor(midgreen) fcolor(midgreen%70)) ///
			(bar p_adopt x if bin==1, barwidth(0.175) lcolor(olive) fcolor(olive%40)) ///
			(bar p_adopt x if bin==2, barwidth(0.175) lcolor(purple) fcolor(purple%20)) /// 
			(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black)) ///
			(pci 	`y1start_01' `x1_01' `yend_01' `x1_01'  `y2start_01' `x2_01' `yend_01' `x2_01'  `yend_01' `x1_01' `yend_01' `x2_01' ///
					`y1start_12' `x1_12' `yend_12' `x1_12'  `y2start_12' `x2_12' `yend_12' `x2_12'  `yend_12' `x1_12' `yend_12' `x2_12' ///
					`y1start_02' `x1_02' `yend_02' `x1_02'  `y2start_02' `x2_02' `yend_02' `x2_02'  `yend_02' `x1_02' `yend_02' `x2_02' ///
					/// 
					, lcolor(gray%50)) ///
			, ///
			graphregion(color(white)) bgcolor(white) ///
			xlabel(0.3 `" "Zero or" "negative" "' 0.6 `" "Positive" "insignificant" "' 0.9 `" "Positive" "significant" "', noticks) /// 
			xscale(range(0.15 `mx')) /// 
			yscale(range(0 100)) yticks(0(10)100) ylabel(0(10)100) ///
			legend(off) /// 
			text(`yend_01' `xcent_01' "{it:p}=`pval_01'", size(small) placement(n)) ///
			text(`yend_12' `xcent_12' "{it:p}=`pval_12'", size(small) placement(n)) ///
			text(`yend_02' `xcent_02' "{it:p}=`pval_02'", size(small) placement(n)) ///
			xtitle(" ") ytitle("Percent of trials recommended by BIT for adoption") /// 
			note("All {it:p}-values calculated using Fisher's exact test")
	graph export "figures/appendix/adoption_by_rec_tbin.pdf", replace
}

*************************************************
***FIGURE A5: ADDITIONAL PRE-EXISTENCE RESULTS***
*************************************************
{ 
	{ // A5a
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1

		mat bar=J(3,7,.) 
		mat colnames bar = bin p_adopt lb ub N N_adopt N_nadopt

		qui reg adopted 1.comm_preexisted 0.comm_preexisted#ibn.control_group_comm, nocons cl(cityid)
		mat bar[1,1]=1
		mat bar[1,2]=_b[1.comm_preexisted]
		mat bar[1,3]=_b[1.comm_preexisted]+invttail(e(df_r),0.975)*_se[1.comm_preexisted]
		mat bar[1,4]=_b[1.comm_preexisted]+invttail(e(df_r),0.025)*_se[1.comm_preexisted]
		qui count if comm_preexisted==1
		mat bar[1,5]=r(N)
		qui count if comm_preexisted==1 & adopted==1
		mat bar[1,6]=r(N)
		qui count if comm_preexisted==1 & adopted==0
		mat bar[1,7]=r(N)
		local r=2
		forvalues s=1(-1)0 {
			mat bar[`r',1]=`r'
			mat bar[`r',2]=_b[0.comm_preexisted#`s'.control_group_comm]
			mat bar[`r',3]=_b[0.comm_preexisted#`s'.control_group_comm]+invttail(e(df_r),0.975)*_se[0.comm_preexisted#`s'.control_group_comm]
			mat bar[`r',4]=_b[0.comm_preexisted#`s'.control_group_comm]+invttail(e(df_r),0.025)*_se[0.comm_preexisted#`s'.control_group_comm]
			qui count if comm_preexisted==0 & control_group_comm==`s'
			mat bar[`r',5]=r(N)
			qui count if comm_preexisted==0 & control_group_comm==`s' & adopted==1
			mat bar[`r',6]=r(N)
			qui count if comm_preexisted==0 & control_group_comm==`s' & adopted==0
			mat bar[`r',7]=r(N)
			local ++r
		}

		*pvals
		qui test 1.comm_preexisted=0.comm_preexisted#1.control_group_comm
		local pval_101=string(`r(p)',"%3.2f")
		qui test 1.comm_preexisted=0.comm_preexisted#0.control_group_comm
		local pval_100=string(`r(p)',"%3.2f")
		qui test 0.comm_preexisted#1.control_group_comm=0.comm_preexisted#0.control_group_comm
		local pval_0100=string(`r(p)',"%3.2f")

		clear
		svmat bar, names(col)
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		qui sum p_adopt
		local maxy=(ceil(`r(max)'/10)+1)*10
		
		egen max=max(p_adopt), by(bin)
		replace max=max+1
		
		sort bin
		gen 	x=_n*0.3
		replace x=x+0.15 if bin>3
		qui sum x if bin<=3
		local mx=`r(max)'+0.15
		
		gen byte zero=0
		
		local group1 1
		local group2 2
		qui sum p_adopt if bin==`group1'
		local y1start_101=`r(max)'+1
		qui sum p_adopt if bin==`group2'
		local y2start_101=`r(max)'+1
		local y1start_101=max(`y1start_101',`y2start_101')
		local y2start_101=`y1start_101'
		local yend_101=`y1start_101'+3
		qui sum x if bin==`group1'
		local x1_101=`r(mean)'
		qui sum x if bin==`group2'
		local x2_101=`r(mean)'
		local xcent_101=(`x1_101'+`x2_101')/2
		
		local group1 1
		local group2 3
		qui sum p_adopt if bin==`group1'
		local y1start_100=`r(max)'+1
		qui sum p_adopt if bin==`group2'
		local y2start_100=`r(max)'+1
		local y1start_100=max(`y1start_100',`y2start_100',`y1start_101'+5)
		local y2start_100=`y1start_100'
		local yend_100=`y1start_100'+3
		qui sum x if bin==`group1'
		local x1_100=`r(mean)'
		qui sum x if bin==`group2'
		local x2_100=`r(mean)'
		local xcent_100=(`x1_100'+`x2_100')/2
		
		local group1 2
		local group2 3
		qui sum p_adopt if bin==`group1'
		local y1start_0100=`r(max)'+1
		qui sum p_adopt if bin==`group2'
		local y2start_0100=`r(max)'+1
		local y1start_0100=max(`y1start_0100',`y2start_0100')
		local y2start_0100=`y1start_0100'
		local yend_0100=`y1start_0100'+3
		qui sum x if bin==`group1'
		local x1_0100=`r(mean)'
		qui sum x if bin==`group2'
		local x2_0100=`r(mean)'
		local xcent_0100=(`x1_0100'+`x2_0100')/2
		
		twoway 	(bar p_adopt x if bin==1, barwidth(0.175) lcolor(midgreen) fcolor(midgreen%70)) ///
				(bar p_adopt x if bin==2, barwidth(0.175) lcolor(olive) fcolor(olive%40)) ///
				(bar p_adopt x if bin==3, barwidth(0.175) lcolor(purple) fcolor(purple%20)) /// 
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black)) ///
				(pci 	`y1start_101' `x1_101' `yend_101' `x1_101'  `y2start_101' `x2_101' `yend_101' `x2_101'  `yend_101' `x1_101' `yend_101' `x2_101' ///
						`y1start_100' `x1_100' `yend_100' `x1_100'  `y2start_100' `x2_100' `yend_100' `x2_100'  `yend_100' `x1_100' `yend_100' `x2_100' ///
						`y1start_0100' `x1_0100' `yend_0100' `x1_0100'  `y2start_0100' `x2_0100' `yend_0100' `x2_0100'  `yend_0100' `x1_0100' `yend_0100' `x2_0100' ///
						, lcolor(gray%50)) ///
				, ///
				graphregion(color(white)) bgcolor(white) ///
				xlabel(0.3 `" " " " " "{bf:Pre-existing}" "{bf:communication}" "' 0.6 `" "Control group" "messaged" "' 0.75 `" " " " " " " "{bf:New communication}" "' 0.9 `" "Control group" "not messaged" "', noticks) ///
				xscale(range(0.15 0.9)) /// 
				yscale(range(0 80)) yticks(0(10)80) ylabel(0(10)80) ///
				legend(off) ///
				text(`yend_101' `xcent_101' "{it:p}=`pval_101'", size(small) placement(n)) ///
				text(`yend_100' `xcent_100' "{it:p}=`pval_100'", size(small) placement(n)) ///
				text(`yend_0100' `xcent_0100' "{it:p}=`pval_0100'", size(small) placement(n)) ///
				xtitle(" ") ytitle("Percent of trials adopted") ///
				ysize(3) xsize(4)
		graph export "figures/appendix/adoption_by_preexist_controlcomm.pdf", replace
	}
	{ // A5b
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1

		local b01 0
		local b02 1.15

		local b11 0
		local b12 2.25

		gen byte tebin=.
		forvalues pre=0/1 {
			qui sum treatmenteffect_max if comm_preexisted==`pre'
			local minte`pre' `r(min)'
			local maxte`pre' `r(max)'
			replace tebin=0 if treatmenteffect_max<=`b`pre'1' & comm_preexisted==`pre'
			replace	tebin=1 if treatmenteffect_max>`b`pre'1' & treatmenteffect_max<=`b`pre'2' & comm_preexisted==`pre'
			replace	tebin=2 if treatmenteffect_max>`b`pre'2' & comm_preexisted==`pre'
		}

		qui tab tebin, gen(tebinind)

		mat bar=J(6,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt

		local r=1
		forvalues pre=0/1 {
			local b=1
			forvalues bin=1/3 {
				mat bar[`r',1]=`pre'
				qui reg adopted tebinind1-tebinind3 if comm_preexisted==`pre', cl(cityid) nocons
				mat bar[`r',2]=`b'
				mat bar[`r',3]=_b[tebinind`bin']
				mat bar[`r',4]=_b[tebinind`bin']+invttail(e(df_r),0.975)*_se[tebinind`bin']
				mat bar[`r',5]=_b[tebinind`bin']+invttail(e(df_r),0.025)*_se[tebinind`bin']
				qui count if tebinind`bin'==1 & comm_preexisted==`pre'
				mat bar[`r',6]=r(N)
				qui count if tebinind`bin'==1 & adopted ==1 & comm_preexisted==`pre'
				mat bar[`r',7]=r(N)
				qui count if tebinind`bin'==1 & adopted ==0 & comm_preexisted==`pre'
				mat bar[`r',8]=r(N)
				local ++r
				local ++b
			}
		}

		qui reg adopted tebinind3 if comm_preexisted==0 & tebinind2==0, cl(cityid)
		qui test tebinind3=0
		local pval0=string(`r(p)',"%3.2f")

		qui tab adopted tebinind3 if comm_preexisted==1 & tebinind2==0, exact
		local pval1=string(`r(p_exact)',"%3.2f")

		qui reg adopted tebinind3 comm_preexisted 1.tebinind3#1.comm_preexisted if tebinind2==0, cl(cityid)
		qui test 1.tebinind3#1.comm_preexisted=0
		local pvalD=string(`r(p)',"%3.2f")

		clear
		svmat bar, names(col)
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		qui sum p_adopt
		local maxy=(ceil(`r(max)'/10)+1)*10
		
		egen max=max(p_adopt), by(bin1 bin2)
		replace max=max+1
		gen byte zero=0
		
		sort bin1 bin2
		gen 	x=_n*0.3+bin1*0.15
		qui sum x
		local mx=`r(max)'+0.15
		
		qui sum p_adopt if bin1==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin1==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin1==0 & bin2==1
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin1==0 & bin2==3
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==1
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==3
		local ystart11=`r(max)'+2
		qui sum x if bin1==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		qui sum x if bin1==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'
		local Ystart0=`yend0'+4
		local Ystart1=`yend1'+4
		local Yend=max(`yend0',`yend1')+5
		local Xcent=(`xcent0'+`xcent1')/2
		
		twoway 	(bar p_adopt x if bin1==0, barwidth(0.235) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==1, barwidth(0.235) lcolor(midgreen) fcolor(midgreen%70)) ///
				`rcap' ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  `yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  `yend1' `xstart1' `yend1' `xend1' ///
						`Ystart0' `xcent0' `Yend' `xcent0'  `Ystart1' `xcent1' `Yend' `xcent1'  `Yend' `xcent0' `Yend' `xcent1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`Ystart0' `xcent0' "{it:p}=`pval0'", size(small) placement(s)) ///
				text(`Ystart1' `xcent1' "{it:p}=`pval1'*", size(small) placement(s)) /// 
				text(`Yend' `Xcent' "{it:p}=`pvalD'", size(small) placement(n)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 `maxy')) yticks(0(10)`maxy') ylabel(0(10)`maxy') ///
				xlabel(0.3 "[`minte0', `b01']" 0.6 "(`b01', `b02']" 0.9 "(`b02', `maxte0']" 1.35 "[`minte1', `b11']" 1.65 "(`b11', `b12']" 1.95 "(`b12', `maxte1']", noticks labsize(small)) ///
				legend(off) /// legend(order(1 "New communication" 3 "Pre-existing communication")) ///
				text(`maxy' `xcent0' "{bf:New communication}", placement(n)) ///
				text(`maxy' `xcent1' "{bf:Pre-existing communication}", placement(n)) ///
				xtitle(" " "{bf:Range of effect sizes (three bins)}") ytitle("Percent of trials adopted") ///
				note("*Calculated using Fisher's exact test" )
		graph export "figures/appendix/adoption_by_preexist_tebin3.pdf", replace
	}
}

***************************
***FIGURE A6: ROC CURVES***
***************************
{
	clear all
	use "data/BITTrialAdoption.dta"
	keep if sample==1

	cap drop evi pre all
	qui logit adopted treatmenteffect_maxt_sig treatmenteffect_max, cl(cityid)
	predict evi, xb
	qui lroc, nograph
	local eviAUC = string(`r(area)',"%3.2f")
	qui logit adopted comm_preexisted, cl(cityid)
	predict pre, xb
	qui lroc, nograph
	local preAUC = string(`r(area)',"%3.2f")
	qui logit adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert mech_personalmotivation mech_simplification mech_socialcues, cl(cityid)
	predict allnopre, xb
	qui lroc, nograph
	local allnopreAUC = string(`r(area)',"%3.2f")
	qui logit adopted comm_preexisted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert mech_personalmotivation mech_simplification mech_socialcues, cl(cityid)
	predict allpre, xb
	qui lroc, nograph
	local allpreAUC = string(`r(area)',"%3.2f")

	qui roccomp adopted evi pre, nograph
	local diffp1=string(`r(p)',"%3.2f")
	qui roccomp adopted allnopre pre, nograph
	local diffp2=string(`r(p)',"%3.2f")

	roccomp adopted pre evi allnopre, ///
		graph graphregion(color(white)) ///
		plot1opts(mcolor(white%0) lcolor(dkgreen) lwidth(medthick)) ///
		plot2opts(mcolor(white%0) lcolor(maroon) lwidth(medthick) lpattern(shortdash)) ///
		plot3opts(mcolor(white%0) lcolor(navy) lwidth(medthick) lpattern(longdash)) /// 
		rlopts(lcolor(gray%50)) ///
		legend(order(1 "Pre-existence only model (AUC=`preAUC')" 2 "Evidence-based model (AUC=`eviAUC')" 3 "Full model (w/o pre-existence) (AUC=`allnopreAUC')" 4 "45{c 176} line") cols(1) margin(0 10 0 0)) ///
		xtitle("False positive rate for nudge adoption") ytitle("True positive rate for nudge adoption", xoffset(-1)) ///
		ysize(12) xsize(10) ///
		note("{it:p}-value for equality of Area Under the Curve (AUC):" ///
			" Pre-existence only vs. Evidence-based: `diffp1'" ///
			" Pre-existence only vs. Full (w/o pre-existence): `diffp2'" ///
			 )
	graph export "figures/appendix/ROC.pdf", replace
}

*****************************
***FIGURE A7: INTERACTIONS***
*****************************
{
	{ // A7a
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1

		mat bar=J(4,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt

		local r=1
		forvalues s=0/1 {
			forvalues t=0/1 {
				qui reg adopted ibn.treatmenteffect_maxt_sig if staff_same_dpt==`s', nocons cl(cityid)
				mat bar[`r',1]=`s'
				mat bar[`r',2]=`t'
				mat bar[`r',3]=_b[`t'.treatmenteffect_maxt_sig]
				mat bar[`r',4]=_b[`t'.treatmenteffect_maxt_sig]+invttail(e(df_r),0.975)*_se[`t'.treatmenteffect_maxt_sig]
				mat bar[`r',5]=_b[`t'.treatmenteffect_maxt_sig]+invttail(e(df_r),0.025)*_se[`t'.treatmenteffect_maxt_sig]
				qui count if staff_same_dpt==`s' & treatmenteffect_maxt_sig==`t' & adopted !=.
				mat bar[`r',6]=r(N)
				qui count if staff_same_dpt==`s' & treatmenteffect_maxt_sig==`t' & adopted ==1
				mat bar[`r',7]=r(N)
				qui count if staff_same_dpt==`s' & treatmenteffect_maxt_sig==`t' & adopted ==0
				mat bar[`r',8]=r(N)
				local ++r
			}
		}

		qui reg adopted 1.treatmenteffect_maxt_sig if staff_same_dpt==1, cl(cityid)
		local pval1=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_maxt_sig]/_se[1.treatmenteffect_maxt_sig])),"%3.2f")
		qui reg adopted 1.treatmenteffect_maxt_sig if staff_same_dpt==0, cl(cityid)
		local pval0=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_maxt_sig]/_se[1.treatmenteffect_maxt_sig])),"%3.2f")
		qui reg adopted 1.treatmenteffect_maxt_sig##1.staff_same_dpt, cl(cityid)
		local pvald=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_maxt_sig#1.staff_same_dpt]/_se[1.treatmenteffect_maxt_sig#1.staff_same_dpt])),"%3.2f")

		clear
		svmat bar, names(col)
		keep bin1 bin2 p_adopt lb ub N N_adopt N_nadopt
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		local maxy=70
		
		egen max=max(p_adopt), by(bin1 bin2)
		replace max=max+1
		gen byte zero=0
		
		sort bin1 bin2
		gen 	x=_n*0.3+bin1*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		qui sum max if bin1==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin1==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin1==0 & bin2==0
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin1==0 & bin2==1
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==0
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==1
		local ystart11=`r(max)'+2
		qui sum x if bin1==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		qui sum x if bin1==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'
		local Ystart0=`yend0'+4
		local Ystart1=`yend1'+4
		local Yend=max(`yend0',`yend1')+5
		local Xcent=(`xcent0'+`xcent1')/2
		
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  `yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  `yend1' `xstart1' `yend1' `xend1' ///
						`Ystart0' `xcent0' `Yend' `xcent0'  `Ystart1' `xcent1' `Yend' `xcent1'  `Yend' `xcent0' `Yend' `xcent1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`Ystart0' `xcent0' "{it:p}=`pval0'", size(small) placement(s)) ///
				text(`Ystart1' `xcent1' "{it:p}=`pval1'", size(small) placement(s)) /// 
				text(`Yend' `Xcent' "{it:p}=`pvald'", size(small) placement(n)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 `maxy')) yticks(0(10)`maxy') ylabel(0(10)`maxy') ///
				xlabel(0.3 "{it:No}" 0.6 "{it:Yes}" 1.1 "{it:No}" 1.4 "{it:Yes}", noticks) ///
				legend(off) ///
				text(`maxy' `xcent0' "{bf:Staff not retained}", placement(n)) ///
				text(`maxy' `xcent1' "{bf:Staff retained}", placement(n)) ///
				xtitle(" " "{bf:Positive and significant result?}") ytitle("Percent of trials adopted")
		graph export "figures/appendix/adoption_by_sig_staff.pdf", replace
	}
	{ // A7b
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1

		egen treatmenteffect_med_bystaff=median(treatmenteffect_max), by(staff_same_dpt)
		gen byte treatmenteffect_abovemed_bystaff=treatmenteffect_max>=treatmenteffect_med_bystaff

		mat bar=J(4,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt

		local r=1
		forvalues s=0/1 {
			forvalues t=0/1 {
				qui reg adopted ibn.treatmenteffect_abovemed_bystaff if staff_same_dpt==`s', nocons cl(cityid)
				mat bar[`r',1]=`s'
				mat bar[`r',2]=`t'
				mat bar[`r',3]=_b[`t'.treatmenteffect_abovemed_bystaff]
				mat bar[`r',4]=_b[`t'.treatmenteffect_abovemed_bystaff]+invttail(e(df_r),0.975)*_se[`t'.treatmenteffect_abovemed_bystaff]
				mat bar[`r',5]=_b[`t'.treatmenteffect_abovemed_bystaff]+invttail(e(df_r),0.025)*_se[`t'.treatmenteffect_abovemed_bystaff]
				qui count if staff_same_dpt==`s' & treatmenteffect_abovemed_bystaff==`t'
				mat bar[`r',6]=r(N)
				qui count if staff_same_dpt==`s' & treatmenteffect_abovemed_bystaff==`t' & adopted==1
				mat bar[`r',7]=r(N)
				qui count if staff_same_dpt==`s' & treatmenteffect_abovemed_bystaff==`t' & adopted==0
				mat bar[`r',8]=r(N)
				local ++r
			}
		}

		qui reg adopted 1.treatmenteffect_abovemed_bystaff if staff_same_dpt==0, cl(cityid)
		local pval0=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_abovemed_bystaff]/_se[1.treatmenteffect_abovemed_bystaff])),"%3.2f")
		qui reg adopted 1.treatmenteffect_abovemed_bystaff if staff_same_dpt==1, cl(cityid)
		local pval1=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_abovemed_bystaff]/_se[1.treatmenteffect_abovemed_bystaff])),"%3.2f")
		qui reg adopted 1.treatmenteffect_abovemed_bystaff##1.staff_same_dpt, cl(cityid)
		local pvald=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_abovemed_bystaff#1.staff_same_dpt]/_se[1.treatmenteffect_abovemed_bystaff#1.staff_same_dpt])),"%3.2f")

		qui sum treatmenteffect_max if treatmenteffect_abovemed_bystaff==0 & staff_same_dpt==0
		local r00="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"
		qui sum treatmenteffect_max if treatmenteffect_abovemed_bystaff==1 & staff_same_dpt==0
		local r10="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"
		qui sum treatmenteffect_max if treatmenteffect_abovemed_bystaff==0 & staff_same_dpt==1
		local r01="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"
		qui sum treatmenteffect_max if treatmenteffect_abovemed_bystaff==1 & staff_same_dpt==1
		local r11="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"

		clear
		svmat bar, names(col)
		keep bin1 bin2 p_adopt lb ub N N_adopt N_nadopt
		drop if bin1==.
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		qui sum p_adopt
		local maxy=70
		
		gen byte zero=0
		
		sort bin1 bin2
		gen 	x=_n*0.3+bin1*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		qui sum p_adopt if bin1==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin1==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin1==0 & bin2==0
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin1==0 & bin2==1
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==0
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==1
		local ystart11=`r(max)'+2
		qui sum x if bin1==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		qui sum x if bin1==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'
		local Ystart0=`yend0'+4
		local Ystart1=`yend1'+4
		local Yend=max(`yend0',`yend1')+5
		local Xcent=(`xcent0'+`xcent1')/2
		
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  `yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  `yend1' `xstart1' `yend1' `xend1' ///
						`Ystart0' `xcent0' `Yend' `xcent0'  `Ystart1' `xcent1' `Yend' `xcent1'  `Yend' `xcent0' `Yend' `xcent1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`Ystart0' `xcent0' "{it:p}=`pval0'", size(small) placement(s)) ///
				text(`Ystart1' `xcent1' "{it:p}=`pval1'", size(small) placement(s)) /// 
				text(`Yend' `Xcent' "{it:p}=`pvald'", size(small) placement(n)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 `maxy')) yticks(0(10)`maxy') ylabel(0(10)`maxy') ///
				xlabel(0.3 "`r00'" 0.6 "`r10'" 1.1 "`r01'" 1.4 "`r11'", noticks) ///
				legend(off) /// 
				text(`maxy' `xcent0' "{bf:Staff not retained}", placement(n)) ///
				text(`maxy' `xcent1' "{bf:Staff retained}", placement(n)) ///
				xtitle(" " "{bf:Range of effect sizes (split by median)}") ytitle("Percent of trials adopted") ///
				note()
		graph export "figures/appendix/adoption_by_te_staff.pdf", replace
	}
	{ // A7c
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1

		mat bar=J(4,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt

		local r=1
		forvalues s=0/1 {
			forvalues t=0/1 {
				qui reg adopted ibn.comm_preexisted if staff_same_dpt==`s', nocons cl(cityid)
				mat bar[`r',1]=`s'
				mat bar[`r',2]=`t'
				mat bar[`r',3]=_b[`t'.comm_preexisted]
				mat bar[`r',4]=_b[`t'.comm_preexisted]+invttail(e(df_r),0.975)*_se[`t'.comm_preexisted]
				mat bar[`r',5]=_b[`t'.comm_preexisted]+invttail(e(df_r),0.025)*_se[`t'.comm_preexisted]
				qui count if staff_same_dpt==`s' & comm_preexisted==`t'
				mat bar[`r',6]=r(N)
				qui count if staff_same_dpt==`s' & comm_preexisted==`t' & adopted==1
				mat bar[`r',7]=r(N)
				qui count if staff_same_dpt==`s' & comm_preexisted==`t' & adopted==0
				mat bar[`r',8]=r(N)
				local ++r
			}
		}

		qui tab comm_preexisted adopted if staff_same_dpt==0, exact
		local pval0=string(`r(p_exact)',"%3.2f")
		qui reg adopted 1.comm_preexisted if staff_same_dpt==1, cl(cityid)
		local pval1=string(2*ttail(e(df_r),abs(_b[1.comm_preexisted]/_se[1.comm_preexisted])),"%3.2f")
		qui reg adopted 1.comm_preexisted##1.staff_same_dpt, cl(cityid)
		local pvald=string(2*ttail(e(df_r),abs(_b[1.comm_preexisted#1.staff_same_dpt]/_se[1.comm_preexisted#1.staff_same_dpt])),"%3.2f")

		clear
		svmat bar, names(col)
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		qui sum p_adopt
		local maxy=(ceil(`r(max)'/10)+1)*10
		
		egen max=max(p_adopt), by(bin1 bin2)
		replace max=max+1
		gen byte zero=0
		
		sort bin1 bin2
		gen 	x=_n*0.3+bin1*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		qui sum max if bin1==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin1==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin1==0 & bin2==0
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin1==0 & bin2==1
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==0
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==1
		local ystart11=`r(max)'+2
		qui sum x if bin1==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		qui sum x if bin1==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'
		local Ystart0=`yend0'+4
		local Ystart1=`yend1'+4
		local Yend=max(`yend0',`yend1')+5
		local Xcent=(`xcent0'+`xcent1')/2
			
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  `yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  `yend1' `xstart1' `yend1' `xend1' ///
						`Ystart0' `xcent0' `Yend' `xcent0'  `Ystart1' `xcent1' `Yend' `xcent1'  `Yend' `xcent0' `Yend' `xcent1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`Ystart0' `xcent0' "{it:p}=`pval0'*", size(small) placement(s)) ///
				text(`Ystart1' `xcent1' "{it:p}=`pval1'", size(small) placement(s)) /// 
				text(`Yend' `Xcent' "{it:p}=`pvald'", size(small) placement(n)) ///
				text(`maxy' `xcent0' "{bf:Staff not retained}", placement(n)) ///
				text(`maxy' `xcent1' "{bf:Staff retained}", placement(n)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 `maxy')) yticks(0(10)`maxy') ylabel(0(10)`maxy') ///
				xlabel(0.3 "{it:New}" 0.6 "{it:Pre-existing}" 1.1 "{it:New}" 1.4 "{it:Pre-existing}", noticks) ///
				legend(off) ///
				xtitle(" " "{bf:Communication in trial existed before the trial?}") ytitle("Percent of trials adopted") ///
				note("*Calculated using Fisher's exact test" )
		graph export "figures/appendix/adoption_by_staff_preexist.pdf", replace
	}
	{ // A7d
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1

		egen controltakeup_med=median(controltakeup)
		qui sum controltakeup_med
		local controltakeup_med=string(`r(mean)',"%4.2f")
		gen byte controltakeup_abovemed=controltakeup>=controltakeup_med if controltakeup!=.

		mat bar=J(4,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt

		local r=1
		forvalues s=0/1 {
			forvalues t=0/1 {
				qui reg adopted ibn.treatmenteffect_maxt_sig if controltakeup_abovemed==`s', nocons cl(cityid)
				mat bar[`r',1]=`s'
				mat bar[`r',2]=`t'
				mat bar[`r',3]=_b[`t'.treatmenteffect_maxt_sig]
				mat bar[`r',4]=_b[`t'.treatmenteffect_maxt_sig]+invttail(e(df_r),0.975)*_se[`t'.treatmenteffect_maxt_sig]
				mat bar[`r',5]=_b[`t'.treatmenteffect_maxt_sig]+invttail(e(df_r),0.025)*_se[`t'.treatmenteffect_maxt_sig]
				qui count if controltakeup_abovemed==`s' & treatmenteffect_maxt_sig==`t'
				mat bar[`r',6]=r(N)
				qui count if controltakeup_abovemed==`s' & treatmenteffect_maxt_sig==`t' & adopted==1
				mat bar[`r',7]=r(N)
				qui count if controltakeup_abovemed==`s' & treatmenteffect_maxt_sig==`t' & adopted==0
				mat bar[`r',8]=r(N)
				local ++r
			}
		}

		qui reg adopted 1.treatmenteffect_maxt_sig if controltakeup_abovemed==1, cl(cityid)
		local pval1=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_maxt_sig]/_se[1.treatmenteffect_maxt_sig])),"%3.2f")
		qui reg adopted 1.treatmenteffect_maxt_sig if controltakeup_abovemed==0, cl(cityid)
		local pval0=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_maxt_sig]/_se[1.treatmenteffect_maxt_sig])),"%3.2f")
		qui reg adopted 1.treatmenteffect_maxt_sig##1.controltakeup_abovemed, cl(cityid)
		local pvald=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_maxt_sig#1.controltakeup_abovemed]/_se[1.treatmenteffect_maxt_sig#1.controltakeup_abovemed])),"%3.2f")

		clear
		svmat bar, names(col)
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		qui sum p_adopt
		local maxy=70
		
		egen max=max(p_adopt), by(bin1 bin2)
		replace max=max+1
		gen byte zero=0
		
		sort bin1 bin2
		gen 	x=_n*0.3+bin1*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		qui sum max if bin1==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin1==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin1==0 & bin2==0
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin1==0 & bin2==1
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==0
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==1
		local ystart11=`r(max)'+2
		qui sum x if bin1==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		qui sum x if bin1==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'
		local Ystart0=`yend0'+4
		local Ystart1=`yend1'+4
		local Yend=max(`yend0',`yend1')+5
		local Xcent=(`xcent0'+`xcent1')/2
		
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  `yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  `yend1' `xstart1' `yend1' `xend1' ///
						`Ystart0' `xcent0' `Yend' `xcent0'  `Ystart1' `xcent1' `Yend' `xcent1'  `Yend' `xcent0' `Yend' `xcent1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`Ystart0' `xcent0' "{it:p}=`pval0'", size(small) placement(s)) ///
				text(`Ystart1' `xcent1' "{it:p}=`pval1'", size(small) placement(s)) /// 
				text(`Yend' `Xcent' "{it:p}=`pvald'", size(small) placement(n)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 `maxy')) yticks(0(10)`maxy') ylabel(0(10)`maxy') ///
				xlabel(0.3 "{it:No}" 0.6 "{it:Yes}" 1.1 "{it:No}" 1.4 "{it:Yes}", noticks) ///
				legend(off) /// 
				text(`maxy' `xcent0' "{bf:Below median}" "{bf:control take-up ({&lt}`controltakeup_med'%)}", placement(0)) ///
				text(`maxy' `xcent1' "{bf:Above median}" "{bf:control take-up ({&ge}`controltakeup_med'%)}", placement(0)) ///
				xtitle(" " "{bf:Positive and significant result?}") ytitle("Percent of trials adopted")
		graph export "figures/appendix/adoption_by_sig_ct.pdf", replace
	}
	{ // A7e
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1
	
		egen controltakeup_med=median(controltakeup)
		qui sum controltakeup_med
		local controltakeup_med=string(`r(mean)',"%4.2f")
		gen byte controltakeup_abovemed=controltakeup>=controltakeup_med if controltakeup!=.

		mat bar=J(4,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt

		local r=1
		forvalues s=0/1 {
			forvalues t=0/1 {
				qui reg adopted ibn.treatmenteffect_abovemed if controltakeup_abovemed==`s', nocons cl(cityid)
				mat bar[`r',1]=`s'
				mat bar[`r',2]=`t'
				mat bar[`r',3]=_b[`t'.treatmenteffect_abovemed]
				mat bar[`r',4]=_b[`t'.treatmenteffect_abovemed]+invttail(e(df_r),0.975)*_se[`t'.treatmenteffect_abovemed]
				mat bar[`r',5]=_b[`t'.treatmenteffect_abovemed]+invttail(e(df_r),0.025)*_se[`t'.treatmenteffect_abovemed]
				qui count if controltakeup_abovemed==`s' & treatmenteffect_abovemed==`t' & adopted !=.
				mat bar[`r',6]=r(N)
				qui count if controltakeup_abovemed==`s' & treatmenteffect_abovemed==`t' & adopted==1
				mat bar[`r',7]=r(N)
				qui count if controltakeup_abovemed==`s' & treatmenteffect_abovemed==`t' & adopted==0
				mat bar[`r',8]=r(N)
				local ++r
			}
		}

		qui reg adopted 1.treatmenteffect_abovemed if controltakeup_abovemed==0, cl(cityid)
		local pval0=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_abovemed]/_se[1.treatmenteffect_abovemed])),"%3.2f")
		qui reg adopted 1.treatmenteffect_abovemed if controltakeup_abovemed==1, cl(cityid)
		local pval1=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_abovemed]/_se[1.treatmenteffect_abovemed])),"%3.2f")
		qui reg adopted 1.treatmenteffect_abovemed##1.controltakeup_abovemed, cl(cityid)
		local pvald=string(2*ttail(e(df_r),abs(_b[1.treatmenteffect_abovemed#1.controltakeup_abovemed]/_se[1.treatmenteffect_abovemed#1.controltakeup_abovemed])),"%3.2f")

		qui sum treatmenteffect_max if treatmenteffect_abovemed==0 & controltakeup_abovemed==0
		local r00="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"
		qui sum treatmenteffect_max if treatmenteffect_abovemed==1 & controltakeup_abovemed==0
		local r10="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"
		qui sum treatmenteffect_max if treatmenteffect_abovemed==0 & controltakeup_abovemed==1
		local r01="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"
		qui sum treatmenteffect_max if treatmenteffect_abovemed==1 & controltakeup_abovemed==1
		local r11="["+string(`r(min)',"%3.1f")+", "+string(`r(max)',"%3.1f")+"]"

		clear
		svmat bar, names(col)
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		qui sum p_adopt
		local maxy=70
		
		egen max=max(p_adopt), by(bin1 bin2)
		replace max=max+1
		gen byte zero=0
		
		sort bin1 bin2
		gen 	x=_n*0.3+bin1*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		qui sum p_adopt if bin1==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin1==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin1==0 & bin2==0
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin1==0 & bin2==1
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==0
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==1
		local ystart11=`r(max)'+2
		qui sum x if bin1==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		qui sum x if bin1==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'
		local Ystart0=`yend0'+4
		local Ystart1=`yend1'+4
		local Yend=max(`yend0',`yend1')+5
		local Xcent=(`xcent0'+`xcent1')/2
			
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(khaki) fcolor(khaki%30)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(cyan) fcolor(cyan%70)) ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  `yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  `yend1' `xstart1' `yend1' `xend1' ///
						`Ystart0' `xcent0' `Yend' `xcent0'  `Ystart1' `xcent1' `Yend' `xcent1'  `Yend' `xcent0' `Yend' `xcent1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`Ystart0' `xcent0' "{it:p}=`pval0'", size(small) placement(s)) ///
				text(`Ystart1' `xcent1' "{it:p}=`pval1'", size(small) placement(s)) /// 
				text(`Yend' `Xcent' "{it:p}=`pvald'", size(small) placement(n)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 `maxy')) yticks(0(10)`maxy') ylabel(0(10)`maxy') ///
				xlabel(0.3 "`r00'" 0.6 "`r10'" 1.1 "`r01'" 1.4 "`r11'", noticks) ///
				legend(off) ///
				text(`maxy' `xcent0' "{bf:Below median}" "{bf:control take-up ({&lt}`controltakeup_med'%)}", placement(0)) ///
				text(`maxy' `xcent1' "{bf:Above median}" "{bf:control take-up ({&ge}`controltakeup_med'%)}", placement(0)) ///
				xtitle(" " "{bf:Range of effect sizes (split by median)}") ytitle("Percent of trials adopted") ///
				note()
		graph export "figures/appendix/adoption_by_te_ct.pdf", replace
	}
	{ // A7f
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1

		egen controltakeup_med=median(controltakeup)
		qui sum controltakeup_med
		local controltakeup_med=string(`r(mean)',"%4.2f")
		gen byte controltakeup_abovemed=controltakeup>=controltakeup_med if controltakeup!=.

		mat bar=J(4,8,.) 
		mat colnames bar = bin1 bin2 p_adopt lb ub N N_adopt N_nadopt

		local r=1
		forvalues s=0/1 {
			forvalues t=0/1 {
				qui reg adopted ibn.comm_preexisted if controltakeup_abovemed==`s', nocons cl(cityid)
				mat bar[`r',1]=`s'
				mat bar[`r',2]=`t'
				mat bar[`r',3]=_b[`t'.comm_preexisted]
				mat bar[`r',4]=_b[`t'.comm_preexisted]+invttail(e(df_r),0.975)*_se[`t'.comm_preexisted]
				mat bar[`r',5]=_b[`t'.comm_preexisted]+invttail(e(df_r),0.025)*_se[`t'.comm_preexisted]
				qui count if controltakeup_abovemed==`s' & comm_preexisted==`t' & adopted !=.
				mat bar[`r',6]=r(N)
				qui count if controltakeup_abovemed==`s' & comm_preexisted==`t' & adopted==1
				mat bar[`r',7]=r(N)
				qui count if controltakeup_abovemed==`s' & comm_preexisted==`t' & adopted==0
				mat bar[`r',8]=r(N)
				local ++r
			}
		}

		qui tab comm_preexisted adopted if controltakeup_abovemed==0, exact
		local pval0=string(`r(p_exact)',"%3.2f")
		qui reg adopted 1.comm_preexisted if controltakeup_abovemed==1, cl(cityid)
		local pval1=string(2*ttail(e(df_r),abs(_b[1.comm_preexisted]/_se[1.comm_preexisted])),"%3.2f")
		qui reg adopted 1.comm_preexisted##1.controltakeup_abovemed, cl(cityid)
		local pvald=string(2*ttail(e(df_r),abs(_b[1.comm_preexisted#1.controltakeup_abovemed]/_se[1.comm_preexisted#1.controltakeup_abovemed])),"%3.2f")

		clear
		svmat bar, names(col)
		
		replace p_adopt=p_adopt*100
		replace ub=ub*100
		replace lb=lb*100
		
		tostring p_adopt, gen(barlabel) format("%2.0f") force
		replace barlabel=barlabel+"%"
		
		tostring N, replace format("%3.0f") force
		tostring N_adopt, replace format("%3.0f") force
		
		replace barlabel=barlabel+" ("+N_adopt+"/"+N+")"
		
		qui sum p_adopt
		local maxy=(ceil(`r(max)'/10)+1)*10
		
		egen max=max(p_adopt), by(bin1 bin2)
		replace max=max+1
		gen byte zero=0
		
		sort bin1 bin2
		gen 	x=_n*0.3+bin1*0.2
		qui sum x
		local mx=`r(max)'+0.15
		
		qui sum max if bin1==0
		local yend0=`r(max)'+5
		qui sum p_adopt if bin1==1
		local yend1=`r(max)'+5
		qui sum p_adopt if bin1==0 & bin2==0
		local ystart00=`r(max)'+2
		qui sum p_adopt if bin1==0 & bin2==1
		local ystart01=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==0
		local ystart10=`r(max)'+2
		qui sum p_adopt if bin1==1 & bin2==1
		local ystart11=`r(max)'+2
		qui sum x if bin1==0
		local xstart0=`r(min)'
		local xend0=`r(max)'
		local xcent0=`r(mean)'
		qui sum x if bin1==1
		local xstart1=`r(min)'
		local xend1=`r(max)'
		local xcent1=`r(mean)'
		local Ystart0=`yend0'+4
		local Ystart1=`yend1'+4
		local Yend=max(`yend0',`yend1')+5
		local Xcent=(`xcent0'+`xcent1')/2
			
		twoway 	(bar p_adopt x if bin1==0 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==0 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				(bar p_adopt x if bin1==1 & bin2==0, barwidth(0.2) lcolor(orange_red) fcolor(orange_red%30)) ///
				(bar p_adopt x if bin1==1 & bin2==1, barwidth(0.2) lcolor(midgreen) fcolor(midgreen%70)) ///
				(scatter zero x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(pci 	`ystart00' `xstart0' `yend0' `xstart0'  `ystart01' `xend0' `yend0' `xend0'  `yend0' `xstart0' `yend0' `xend0' ///
						`ystart10' `xstart1' `yend1' `xstart1'  `ystart11' `xend1' `yend1' `xend1'  `yend1' `xstart1' `yend1' `xend1' ///
						`Ystart0' `xcent0' `Yend' `xcent0'  `Ystart1' `xcent1' `Yend' `xcent1'  `Yend' `xcent0' `Yend' `xcent1' ///
						, lcolor(gray%50)) ///
				, ///
				text(`Ystart0' `xcent0' "{it:p}=`pval0'*", size(small) placement(s)) ///
				text(`Ystart1' `xcent1' "{it:p}=`pval1'", size(small) placement(s)) /// 
				text(`Yend' `Xcent' "{it:p}=`pvald'", size(small) placement(n)) ///
				text(`maxy' `xcent0' "{bf:Below median}" "{bf:control take-up ({&lt}`controltakeup_med'%)}", placement(0)) ///
				text(`maxy' `xcent1' "{bf:Above median}" "{bf:control take-up ({&ge}`controltakeup_med'%)}", placement(0)) ///
				graphregion(color(white)) bgcolor(white) ///
				xscale(range(0.15 `mx')) yscale(range(0 `maxy')) yticks(0(10)`maxy') ylabel(0(10)`maxy') ///
				xlabel(0.3 "{it:New}" 0.6 "{it:Pre-existing}" 1.1 "{it:New}" 1.4 "{it:Pre-existing}", noticks) ///
				legend(off) ///
				xtitle(" " "{bf:Communication in trial existed before the trial?}") ytitle("Percent of trials adopted") ///
				note("*Calculated using Fisher's exact test" )
		graph export "figures/appendix/adoption_by_ct_preexist.pdf", replace
	}
}

**************************************
***FIGURE A8: SURVEY SPLIT BY GROUP***
**************************************
{
	clear all
	use "data/Survey.dta"

	gen byte group = 0 if adopter==0 & BIT==1
	replace group = 1 if adopter==0 & BIT==0
	replace group = 2 if adopter==1
	drop if group==.
	
	frame copy default temp
	frame change temp
	
	* Bootstrap standard errors
	keep survey* id trialnumber group

	local s=1
	local lab
	foreach var in priority remind techsup stafffte training funding evidence {
		rename survey_`var' survey`s'
		local lab `lab' `s' "`var'"
		local ++s
	}

	greshape long survey, i(id trialnumber) j(s)
	label define lab `lab'
	label values s lab

	frame create bs
	tempfile b

	set seed 123
	forvalues bs=1/1000 {
		if mod(`bs',100)==0 {
			di "Bootstrap `bs' out of 1000"
		}
		qui {
			preserve
				bsample, cl(id) id(bsid)
				gegen trialnumberN=count(id), by(trialnumber s)
				replace trialnumberN=1 if group>0
				gen weight=1/trialnumberN
				gcollapse (mean) survey [aweight=weight], by(group s)
				save `b', replace
				frame change bs
				append using `b'
				frame change temp
			restore
		}
	}

	frame change bs
	gcollapse (sd) survey, by(group s)

	frame change default
	frame drop temp

	mat survey=J(21,7,.)
	mat colnames survey = group factor mean se lb ub overallmean
	local row=1
	local desclist `" "Prioritization from key decision-makers" "Timely reminders" "Logistics and tech support" "More staff FTE hours" "Staff receive training from external consultants" "Funding for communication costs" "Stronger evidence" "'
	local values
	forvalues group=0/2 {
		local row=1
		local s=1
		foreach var in survey_priority survey_remind survey_techsup survey_stafffte survey_training survey_funding survey_evidence {
			local desc: word `row' of `desclist'
			local values `values' `row' "`desc'"
			reg `var' if group==`group' [aweight=weight]
			mat survey[`row'+7*`group',1]=`group'
			mat survey[`row'+7*`group',2]=`row'
			mat survey[`row'+7*`group',3]=_b[_cons]
			frame change bs
				qui sum survey if s==`s' & group==`group'
				qui assert `r(N)'==1
				local se=`r(mean)'
				mat survey[`row'+7*`group',4]=`se'
				mat survey[`row'+7*`group',5]=_b[_cons]-invnormal(0.975)*`se'
				mat survey[`row'+7*`group',6]=_b[_cons]+invnormal(0.975)*`se'
			frame change default
			qui reg `var' [aweight=weight]
			mat survey[`row'+7*`group',7]=_b[_cons]
			local ++row
			local ++s
		}
		
		qui distinct id if group==`group'
		local Ngroup`group'=`r(ndistinct)'
	}

	clear
	svmat survey, names(col)
	label define values `values'
	label values factor values 
	decode factor, gen(factor_name)

	tostring mean, gen(barlabel) format("%3.2f") force

	gsort -overallmean group
	local width=0.3
	gen asd=ceil(_n/2)-1
	gen 	x=_n*`width' + (ceil(_n/3)-1)*`width'/2
	qui sum x
	local center=`r(mean)'
	local xlabel
	forvalues f=2(3)21 {
		local fact=factor_name[`f']
		local wc: word count `fact'
		if `wc'==2 {
			local lab1=word("`fact'",1)
			local lab2=word("`fact'",2)
			local lab3=""
		}
		if `wc'==4 {
			local lab1=word("`fact'",1)+" "+word("`fact'",2)
			local lab2=word("`fact'",3)+" "+word("`fact'",4)
			local lab3=""
		}
		if `wc'==6 {
			local lab1=word("`fact'",1)+" "+word("`fact'",2)
			local lab2=word("`fact'",3)+" "+word("`fact'",4)
			local lab3=word("`fact'",5)+" "+word("`fact'",6)
		}
		
		local pos=x[`f']
		local xlabel `xlabel' `pos' `" "`lab1'" "`lab2'" "`lab3'" "'
	}

	gen ub_tag=(ub>5.25)
	gen lb_tag=(lb<1)
	replace ub=5.25 if ub>5.25
	replace lb=1 if lb<1

	local lb_cen
	local ub_cen
	local N=_N
	forvalues f=1/`N' { // manual rcaps
		if lb_tag[`f']==1 & ub_tag[`f']!=1 {
			local ub=ub[`f']
			local x=x[`f']
			local x1=`x'-(`width'/4)
			local x2=`x'+(`width'/4)
			local ub_cen `ub_cen' `ub' `x1' `ub' `x2'
		}
		if lb_tag[`f']!=1 & ub_tag[`f']==1 {
			local lb=lb[`f']
			local x=x[`f']
			local x1=`x'-(`width'/4)
			local x2=`x'+(`width'/4)
			local lb_cen `lb_cen' `lb' `x1' `lb' `x2'
		}
	}
	* in case no censored
	if "`lb_cen'"=="" {
		local lb_cen 1 1 1.5 1.5, lwidth(none)
	}
	else {
		local lb_cen `lb_cen', lcolor(gray%50)
	}
	if "`ub_cen'"=="" {
		local ub_cen 1 1 1.5 1.5, lwidth(none)
	}
	else {
		local ub_cen `ub_cen', lcolor(gray%50)
	}

	gen one=1
	qui sum ub
	local title=`r(max)'+0.2

	twoway 	(bar mean x if group==0, barwidth(`width') lcolor(cranberry) fcolor(cranberry%50) base(1)) ///
			(bar mean x if group==1, barwidth(`width') lcolor(purple) fcolor(purple%70) base(1)) ///
			(bar mean x if group==2, barwidth(`width') lcolor(lime) fcolor(lime%70) base(1)) ///
			(scatter one x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
			(rcap lb ub x if lb_tag!=1 & ub_tag!=1, msize(5) lcolor(gray%50)) ///
			, ///
			graphregion(color(white) margin(0 0 0 5)) ///
			xlabel(`xlabel', noticks labsize(small)) ///
			ylabel(1 "1 - Not at all" 2 "2 - Slightly" 3 "3 - Moderately" 4 "4 - Very" 5 "5 - Extremely", labsize(small) angle(0)) ///
			xsize(10) ysize(5) ///
			xtitle("") ytitle("") ///
			legend(order(1 "BIT non-adopters ({it:N}=`Ngroup0')" 2 "Non-BIT non-adopters ({it:N}=`Ngroup1')" 3 "Non-BIT adopters ({it:N}=`Ngroup2')") cols(3)) ///
			text(5.25 `center' "{bf:How helpful would each of the following be for supporting a city to send the communication routinely?}", placement(n))
	graph export "figures/appendix/survey_by_group.pdf", replace
}

*****************************************
***FIGURE A10: COMPARISON OF FORECASTS***
*****************************************
{ 
	{ // A10a
		clear all
		use "data/BITTrialAdoption.dta"
		keep if sample==1
		append using "data/Forecasts.dta"
		replace cityid=1000+_n if missing(cityid)

		gen adopt_bysig_diff_f = adopt_bysig_1-(adopt_bysig_2*(24/(24+16))+adopt_bysig_3*(16/(24+16))) // weighted average of other two
		gen		tbin=-1 if treatmenteffect_maxt<=0 & sample==1
		replace	tbin= 0 if treatmenteffect_maxt>0 & treatmenteffect_maxt<invnorm(0.975) & sample==1
		replace	tbin= 1 if treatmenteffect_maxt>=invnorm(0.975) & sample==1
		tab tbin, gen(tbinind)
		local c1 qui reg adopted tbinind3
		local coef1 _b[tbinind3]*100
		local lc1 qui lincom [r1_mean]_cons-100*[s1_mean]tbinind3

		gen adopt_byte_diff_f = adopt_byte_1-(adopt_byte_2*(25/(24+25))+adopt_bysig_3*(24/(24+25))) // weighted average of other two
		local b1 0.19
		local b2 1.7
		gen 	tebin=0 if treatmenteffect_max<=`b1' & sample==1
		replace	tebin=1 if treatmenteffect_max>`b1' & treatmenteffect_max<=`b2' & sample==1
		replace	tebin=2 if treatmenteffect_max>`b2' & sample==1
		tab tebin, gen(tebinind)
		local c2 qui reg adopted tebinind3
		local coef2 _b[tebinind3]*100
		local lc2 qui lincom [r2_mean]_cons-100*[s2_mean]tebinind3

		gen adopt_bystaff_diff_f = adopt_bystaff_1-adopt_bystaff_2
		local c3 qui reg adopted staff_same_dpt
		local coef3 100*_b[staff_same_dpt]
		local lc3 qui lincom [r3_mean]_cons-100*[s3_mean]staff_same_dpt

		gen adopt_bysize_diff_f = adopt_bysize_1-adopt_bysize_2
		local c4 qui reg adopted pop_abovemed
		local coef4 100*_b[pop_abovemed]
		local lc4 qui lincom [r4_mean]_cons-100*[s4_mean]pop_abovemed

		gen adopt_byWWC_diff_f = adopt_byWWC_1-adopt_byWWC_2
		local c5 qui reg adopted WWC_cert
		local coef5 100*_b[WWC_cert]
		local lc5 qui lincom [r5_mean]_cons-100*[s5_mean]WWC_cert

		gen adopt_bymech_diff_f = adopt_bymech_1-(adopt_bymech_2+adopt_bymech_3)/2 // just average other two because not MECE
		gen mechbin1=(mech_simplification==1)
		gen mechbin23=(mech_personalmotivation==1 | mech_socialcues==1)
		local c6 qui reg adopted mechbin1 if (mechbin1==1 | mechbin23==1)
		local coef6 _b[mechbin1]*100
		local lc6 qui lincom [r6_mean]_cons-100*[s6_mean]mechbin1

		gen adopt_bypreexist_diff_f = adopt_bypreexist_2-adopt_bypreexist_1
		local c7 qui reg adopted comm_preexisted
		local coef7 100*_b[comm_preexisted]
		local lc7 qui lincom [r7_mean]_cons-100*[s7_mean]comm_preexisted

		local varlist adopt_bysig_diff_f adopt_byte_diff_f adopt_bystaff_diff_f adopt_bysize_diff_f adopt_byWWC_diff_f adopt_bymech_diff_f adopt_bypreexist_diff_f
		preserve
			keep `varlist'
			drop if adopt_bysig_diff_f==.
			local varlabel
			local v=1
			foreach var in `varlist' {
				rename `var' adopt_diff_dim`v'
				local varlabel `varlabel' `v' "`var'"
				local ++v
			}
			gen n=_n
			reshape long adopt_diff_dim, i(n) j(dim)
			label define dim `varlabel'
			label values dim dim
			egen dim_rank=rank(adopt_diff_dim), by(n)
			replace dim_rank=8-dim_rank
		restore

		local varcount: word count `varlist'

		mat scatter=J(`varcount',4,.)
		mat colnames scatter = dim forecast actual se

		local i=1
		foreach var in `varlist' {
			mat scatter[`i',1]=`i'
			
			qui reg `var'
			mat scatter[`i',2]=_b[_cons]
			est sto r`i'
			
			`c`i''
			mat scatter[`i',3]=`coef`i''
			est sto s`i'
			
			qui suest r`i' s`i', cl(cityid)
			`lc`i''
			mat scatter[`i',4]=`r(se)'
			local ++i
		}

		svmat scatter, names(col)
		label define dim 1 "Positive significant" 2 "High effect size" 3 "Staff retention" 4 "State capacity" 5 "Certification" 6 "Simplification" 7 "Pre-existing"
		label values dim dim

		gen lb=actual-invnorm(0.975)*se
		gen ub=actual+invnorm(0.975)*se

		qui sum lb
		local min=`r(min)'
		qui sum ub
		local max=`r(max)'
		qui sum actual
		local min=min(`r(min)',`min')
		local max=max(`r(max)',`max')

		gen n=_n
		gen N=_N
		replace n=`min' if n==1
		replace n=`max' if n==N

		gen x=n
		replace x=30 if x>30
		replace x=0 if x<0

		qui sum lb if dim==7
		local low=`r(mean)'
		qui sum forecast if dim==7
		local x=`r(mean)'
		local xL=`x'-0.3
		local xR=`x'+0.3

		twoway (line x x, lpattern(dash) lcolor(gray%50)) ///
				(rcap lb ub forecast if dim!=7, lcolor(navy%50) lwidth(medium)) ///
				(lfit actual forecast, lcolor(maroon%70)) ///
				(pci `low' `x' 60 `x'  `low' `xL' `low' `xR', lcolor(navy%50) lwidth(medium)) ///
				(scatter actual forecast if dim!=3 & dim!=2, msym(triangle) mcolor(navy) mlabel(dim) mlabpos(3) mlabcol(navy)) ///
				(scatter actual forecast if dim==3, msym(triangle) mcolor(navy) mlabel(dim) mlabpos(9) mlabcol(navy)) ///		
				(scatter actual forecast if dim==2, msym(triangle) mcolor(navy) mlabel(dim) mlabpos(3) mlabcol(navy)) ///		
				, ///
				xline(0, lcolor(gray)) yline(0, lcolor(gray)) ///
				graphregion(color(white) margin(0 20 0 0)) ///
				xlabel(0(10)30) ylabel(-20(10)60) ///
				xtitle("Forecasted difference in adoption (pp.)") ///
				ytitle("Observed difference in adoption (pp.)") ///
				legend(order(1 "45{c 176} line" 3 "Fitted line") ring(0) position(2) cols(1)) ///
				note(`" 95% confidence intervals are shown for the "difference-in-difference" in each dimension "')
		graph export "figures/appendix/scatter_forecast_observed.pdf", replace
	}
	{ // A10b
		clear all
		use "data/Forecasts.dta"

		mat bar=J(6,6,.) 
		mat colnames bar = bin cat p_adopt lb ub N

		forvalues s=1(1)3 {
			qui reg compare_RCToverall_`s', r
			mat bar[`s',1]=`s'
			mat bar[`s',2]=1
			mat bar[`s',3]=_b[_cons]
			mat bar[`s',4]=_b[_cons]+invttail(e(df_r),0.975)*_se[_cons]
			mat bar[`s',5]=_b[_cons]+invttail(e(df_r),0.025)*_se[_cons]
			qui count if compare_RCToverall_`s'!=.
			mat bar[`s',6]=r(N)
			qui reg compare_RCTsig_`s', r
			mat bar[`s'+3,1]=`s'
			mat bar[`s'+3,2]=2
			mat bar[`s'+3,3]=_b[_cons]
			mat bar[`s'+3,4]=_b[_cons]+invttail(e(df_r),0.975)*_se[_cons]
			mat bar[`s'+3,5]=_b[_cons]+invttail(e(df_r),0.025)*_se[_cons]
			qui count if compare_RCTsig_`s'!=.
			mat bar[`s'+3,6]=r(N)
		}

		clear
		svmat bar, names(col)
		
		tostring p_adopt, gen(barlabel) format("%3.2f") force
		
		gen 	x=_n*0.3
		replace x=x+0.15 if cat==2
		qui sum x
		local mx = `r(max)'+0.15
			
		gen byte zero=0
		gen one=1
		
		twoway 	(bar p_adopt x if cat==1, barwidth(0.2) lcolor(emidblue) fcolor(emidblue%70)) ///
				(bar p_adopt x if cat==2, barwidth(0.2) lcolor(stone) fcolor(stone%30)) ///
				(scatter one x, msym(none) mlabel(barlabel) mlabpos(12) mlabcolor(black) mlabsize(small)) ///
				(rcap lb ub x, msize(10) lcolor(gray%50)) ///
				, ///
				graphregion(color(white)) bgcolor(white) ///
				xlabel(0.3 `" "A/B tests in" "large firms" "' 0.6 `" "Nudges in" "US cities" "' 0.9 `" "Dev. RCTs in" "low-inc. countries" "' 1.35 `" "A/B tests in" "large firms" "' 1.65 `" "Dev. RCTs in" "low-inc. countries" "' 1.95 `" "Nudges in" "US cities" "' , noticks labsize(small)) ///
				xscale(range(0.15 `mx')) yscale(range(1 3)) yticks(1(0.5)3) ylabel(1(0.5)3) ///
				xtitle("") ytitle("Average ranking (1-highest, 3-lowest)") ///
				legend(off) ///
				text(3 0.6 "{bf:Adoption of trial interventions}", placement(s)) ///
				text(3 1.65 "{bf:Responsiveness to effect size}", placement(s)) ///
				note("95% confidence intervals shown")			
		graph export "figures/appendix/forecasts_ranking.pdf", replace
	}
}